<?php
namespace app\api\controller;
use think\Input;
use think\Controller;
use app\admin\model\Networks;
use app\admin\model\Clouds;
use app\admin\model\Messages;
use app\admin\model\Users;
use app\admin\model\Takes;
use app\common\controller\Base;
class Notice extends Base{
	public function _initialize(){
        parent::_initialize();
        $this->Network = new Networks;
        $this->Takes = new Takes;
    }
	/*提前一个月提醒过期提醒*/
    public function Month(){
        $map['m.status'] = array('in',[2,8,10]);
        $time = time()+(30*86400);
        $endTime = strtotime(date('Y-m-d',$time));
        //$endTime = $startTiome+86399;
        $map['enddate'] = $endTime;
        $lists = $this->Network->alias('m')
        ->where($map)
        ->field("*,local_cloud_id as locallabel,endcloudid as endclou,type as locatlabeladdress,status as statuslabel,type as typelabel,appro_time as approtime,endtype as endlabeladdress")
        ->order('enddate asc')->select();
        $this->assign('url',U('/'));
        $this->assign('day',30);
        $this->Network->getLastSql();
        if($lists){
        	$us = [];
        	foreach($lists as $vo){
        		if(!isset($us[$vo['user_id']])){
        			$us[$vo['user_id']] = [];
        		}
                $vo['email'] = Users::getEmail($vo['user_id']);
        		array_push($us[$vo['user_id']], $vo);
        	}
        	foreach($us as $user_id=>$items){
        		$this->assign('lists',$items);
                $items['id'];
                $this->assign('isType',1);
        		$this->assign('email',Users::getEmail($user_id));
        		$html = $this->fetch('list');
        		Messages::Notice((int)$user_id,lang('Line expiration reminder'),$html,3,0);
        	}
            $this->assign('lists',$lists);
            $this->assign('isType',2);
        	$html = $this->fetch('list');
            Messages::Notice(0,lang('Line expiration reminder'),$html,3,1);
            
        }
       
    }
    /*提前一个周提醒过期提醒*/
    public function weeks(){
        $map['m.status'] = array('in',[2,8,10]);
        $time = time()+(7*86400);
        $endTime = strtotime(date('Y-m-d',$time));
        $map['enddate'] = $endTime;
        $lists = $this->Network->alias('m')
        ->where($map)
        ->field("*,local_cloud_id as locallabel,endcloudid as endclou,type as locatlabeladdress,status as statuslabel,type as typelabel,appro_time as approtime,endtype as endlabeladdress")
        ->order('enddate asc')->select();
        //echo $this->Network->getLastSql();exit;
        $this->assign('url',U('/'));
        $this->assign('day',7);
        if($lists){
        	$us = [];
        	foreach($lists as $vo){
        		if(!isset($us[$vo['user_id']])){
        			$us[$vo['user_id']] = [];
        		}
                $vo['email'] = Users::getEmail($vo['user_id']);
        		array_push($us[$vo['user_id']], $vo);
        	}
        	foreach($us as $user_id=>$items){
        		$this->assign('lists',$items);
                $this->assign('isType',1);
        		$this->assign('email',Users::getEmail($user_id));
        		$html = $this->fetch('list');
                Messages::Notice((int)$user_id,lang('Line expiration reminder'),$html,3,0);
        	}
        	$this->assign('lists',$lists);
            $this->assign('isType',2);
            $html = $this->fetch('list');
            Messages::Notice(0,lang('Line expiration reminder'),$html,3,1);
        }
    }
    /*过期*/
    public function outtime(){
        $map['status'] = array('in',[2,8,10,15]);
        $endTime = strtotime(date('Y-m-d',time()-86400));
        $map['enddate'] = $endTime;
        $lists = $this->Network
        ->where($map)
        ->field("*,local_cloud_id as locallabel,endcloudid as endclou,type as locatlabeladdress,status as statuslabel,type as typelabel,appro_time as approtime,endtype as endlabeladdress")
        ->order('enddate asc')->select();
        //echo $this->Network->getLastSql();exit;
        $this->assign('url',U('/'));
        $this->assign('day',0);
        if($lists){
            $us = [];
            foreach($lists as $vo){
                if(!isset($us[$vo['user_id']])){
                    $us[$vo['user_id']] = [];
                }
                $vo['email'] = Users::getEmail($vo['user_id']);
                array_push($us[$vo['user_id']], $vo); 
            }
            foreach($us as $user_id=>$items){
                $this->assign('lists',$items);
                $this->assign('isType',1);
                $this->assign('username',Users::getUsername($user_id));
                $htmls = $this->fetch('list');
                Messages::Notice($user_id,lang('Line expiration reminder'),$htmls,3,false);
            }
            
        }
        $this->assign('lists',$lists);
        $this->assign('isType',2);
        $html = $this->fetch('list');
        Messages::Notice(0,lang('Line expiration reminder'),$html,3,1);
    }
}